<!-- NProgress -->
<link href="/vendors/nprogress/nprogress.css" rel="stylesheet"/>
<!-- 上传图片 -->
<link href="/vendors/fileinput/fileinput.min.css" rel="stylesheet"/>

<!-- page content -->
<div id="profileDiv" class="right_col" role="main">
    <div class="">
        <div class="page-title">
            <div class="title_left">
                <h3>User Profile</h3>
            </div>

            <div class="title_right">
                <div class="col-md-5 col-sm-5 form-group pull-right top_search">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for..."/>
                        <span class="input-group-btn">
							<button class="btn btn-secondary" type="button">Go!</button>
						</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="clearfix"></div>

        <div class="row">
            <div class="col-md-12 col-sm-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>User Report <small>Activity report</small></h2>
                        <ul class="nav navbar-right panel_toolbox">
                            <li>
                                <a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                   aria-expanded="false"><i class="fa fa-wrench"></i></a>
                                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                    <a class="dropdown-item" href="#">Settings 1</a>
                                    <a class="dropdown-item" href="#">Settings 2</a>
                                </div>
                            </li>
                            <li>
                                <a class="close-link"><i class="fa fa-close"></i></a>
                            </li>
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                        <div class="col-md-3 col-sm-3 profile_left">
                            <div class="profile_img">
                                <div id="crop-avatar">
                                    <!-- Current avatar -->

                                    <img class="img-responsive avatar-view" :src="user.userImage" alt="Avatar"
                                         title="Change the avatar"/>
                                </div>
                            </div>
                            <h3>{{user.userName}}</h3>

                            <ul class="list-unstyled user_data">
                                <li>
                                    <i class="fa fa-paper-plane-o user-profile-icon"></i> {{user.email}}
                                </li>

                                <li><i class="fa  fa-clock-o user-profile-icon"></i> {{user.createDate}}</li>

                                <li class="m-top-xs">
                                    <i class="fa  fa-group (alias) user-profile-icon"></i>
                                    <div v-for="role in user.roles" style="display:inline-block;">&nbsp;
                                        <el-tag size="mini"
                                                :type="role.roleName === 'admin' ? 'primary' : role.roleName === 'superAdmin' ? 'danger':'success'"
                                                disable-transitions>
                                            {{role.roleName === 'admin' ? '管理员' : role.roleName === 'superAdmin' ?
                                            '超级管理员':'普通用户'}}
                                        </el-tag>
                                    </div>
                                </li>
                            </ul>
                            <a data-toggle="modal" data-target="#profileEdit" class="btn btn-success"><i
                                    class="fa fa-edit m-right-xs"></i>Edit Profile</a>
                            <br/>
                        </div>
                        <div class="col-md-9 col-sm-9">
                            <div class="profile_title">
                                <div class="col-md-6">
                                    <h2>User Activity Report</h2>
                                </div>
                                <div class="col-md-6">
                                    <div id="reportrange" class="pull-right"
                                         style="margin-top: 5px; cursor: pointer; padding: 5px 10px"></div>
                                </div>
                            </div>
                            <!-- start of user-activity-graph -->
                            <div id="graph_bar" style="width: 100%; height: 280px"></div>
                            <!-- end of user-activity-graph -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div th:replace="account/profileEdit"></div>
    <!-- /page content -->
</div>

<!-- NProgress -->
<script src="/vendors/nprogress/nprogress.js" type="text/javascript"></script>
<!-- morris.js -->
<script src="/vendors/raphael/raphael.min.js" type="text/javascript"></script>
<script src="/vendors/morris.js/morris.min.js" type="text/javascript"></script>
<!-- bootstrap-progressbar -->
<script src="/vendors/bootstrap-progressbar/bootstrap-progressbar.min.js" type="text/javascript"></script>
<!-- 上传图片 -->
<script src="/vendors/fileinput/fileinput.min.js" type="text/javascript"></script>

<script>
    var profileVue = new Vue({
        el: "#profileDiv",
        data: {
            user: {
                userName: "",
                email: "",
                userImage: "",
                imageType: "profile-big",
            },
            imageTypes: [],
            editPassword:{
                password:"",
                newPassword:""
            }
        },
        methods: {
            initProfile: function () {
                var userId = $("#userId").val();
                console.log("userId:", userId);
                var self = this;
                $.ajax({
                    url: "/api/user/" + userId,
                    type: "get",
                    success: function (res) {
                        self.user = res.data;
                        if (res.data.userImage === "" || res.data.userImage == null) {
                            self.user.userImage = "/images/img.jpg";
                        }
                        self.user.imageType = "profile-big";
                    },
                    error: function (err) {
                        self.$message({
                            type: 'error',
                            message: err.responseText
                        });
                    },
                });
            },
            initImageTypes: function () {
                var self = this;
                $.ajax({
                    url: "/api/dictionary/imageType",
                    type: "get",
                    success: function (rs) {
                        self.imageTypes = rs;
                    },
                    error: function (err) {
                        self.$message({
                            type: 'error',
                            message: err.responseText
                        });
                    },
                });
            },
            uploadImage: function () {
                var self = this;
                $("#uploadImage")
                    .fileinput({
                        uploadUrl: "/api/image/" + self.user.imageType,
                        enctype: "multipart/form-data",
                        previewFileType: "image",
                        uploadAsync: true,
                        allowedFileExtensions: ["png", "jpg", "jpeg", "ico", "bmp", "gif"],
                        maxFileCount: 1,
                        maxFileSize: $("[name='imageTypes']").find("option:selected").attr("size"),
                        minImageWidth: 10,
                        minImageHeight: 10,
                        maxImageWidth: $("[name='imageTypes']").find("option:selected").attr("length"),
                        maxImageHeight: $("[name='imageTypes']").find("option:selected").attr("height"),
                        msgFilesTooMany: "Upload file count({n} - {m})",
                        showCaption: false,
                        dropZoneEnabled: false,
                        showBrowse: true,
                        browseClass: "btn btn-primary",
                        uploadClass: "btn btn-info",
                        removeClass: "btn btn-danger",
                    })
                    .on("fileerror", function (event, data, msg) {
                        self.$message({
                            type: 'error',
                            message: "Upload file failed！"
                        });
                    })
                    .on("fileuploaded", function (event, data) {
                        if (data.response.status == 200) {
                            self.user.userImage = data.response.data;
                        } else {
                            $(".fileinput-remove-button").click();
                        }
                        self.$message({
                            type: 'success',
                            message: data.response.message
                        });
                    })
                    .on("fileclear", function (event) {
                    });
            },
            refreshFileInput: function () {
                $("#uploadImage").fileinput("refresh", {
                    maxFileSize: $("[name='imageTypes']").find("option:selected").attr("size"),
                    maxImageWidth: $("[name='imageTypes']").find("option:selected").attr("length"),
                    maxImageHeight: $("[name='imageTypes']").find("option:selected").attr("height"),
                });
            },
            updateModel: function () {
                if (this.user.userName === "" || this.user.email === "") {
                    this.$message({
                        type: 'error',
                        message: "Please input user info."
                    });
                    return;
                }
                var self = this;
                $.ajax({
                    url: "/api/user",
                    type: "put",
                    contentType: "application/json",
                    data: JSON.stringify(self.user),
                    success: function (res) {
                        if (res.status === 200) {
                            self.$message({
                                type: 'success',
                                message: res.message
                            });
                            $("[name=userImage]").attr("src", self.user.userImage);
                            $("[name=userName]").html(self.user.userName);
                            $("#profileEdit").modal("hide");
                        } else {
                            self.$message({
                                type: 'info',
                                message: res.message
                            });
                        }
                    },
                    error: function (err) {
                        self.$message({
                            type: 'error',
                            message: "update faild!"
                        });
                    },
                });
            },
        },
        mounted: function () {
            this.initProfile();
            this.initImageTypes();
            this.uploadImage();
        },
        updated: function () {
            this.refreshFileInput();
        },
    });
</script>